import mysql from 'mysql2/promise';
import fs from 'fs';
import path from 'path';

async function saveSongPathToDatabase() {
    const pool = mysql.createPool({
        host: 'localhost',
        user: 'root',
        password: '123456',
        database: 'musiccenter',
        port: 3306,
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0,
        connectTimeout: 30000
    });

    const connection = await pool.getConnection();

    try {
        const songPath = 'D:\\音乐中枢\\Songs\\goldwind.mp3';
        const songTitle = 'Goldwind';
        const songAuthor = '周杰伦';
        const filename = path.basename(songPath); // 只获取文件名

        const [result] = await connection.execute(
            'INSERT INTO songs (songtitle, songauthor, songpath) VALUES (?, ?, ?)',
            [songTitle, songAuthor, filename]
        );

        console.log('Song path saved to database with ID:', result.insertId);
    } catch (error) {
        console.error('Error saving song path to database:', error);
    } finally {
        try {
            await connection.release();
        } catch (endError) {
            console.error('Error releasing database connection:', endError);
        }
    }
}

saveSongPathToDatabase();